import {ModalForm, ProFormSelect, ProFormText, ProFormTextArea,} from '@ant-design/pro-components';
import React from 'react';

export type UpdateFormProps = {
  onCancel: (flag?: boolean, formVals?: API.SystemParameter) => void;
  onSubmit: (values: API.SystemParameter) => Promise<void>;
  updateModalOpen: boolean;
  values?: Partial<API.SystemParameter>;
};

const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  return (<ModalForm
      title="编辑"
      width="700px"
      layout={'horizontal'}
      labelCol={{span:6}}
      open={props.updateModalOpen}
      onFinish={props.onSubmit}
      modalProps={{destroyOnClose: true, onCancel: () => props.onCancel()}}
    >
      <ProFormText
        initialValue={props.values?.systemSchema}
        rules={[
          {
            required: true,
            message: '系统schema不能为空',
          },
        ]}
        width="md"
        label="系统schema"
        name="systemSchema"
      />
      <ProFormText
        initialValue={props.values?.systemMarkTable}
        rules={[
          {
            required: true,
            message: '双向同步标记表不能为空',
          },
        ]}
        width="md"
        label="双向同步标记表"
        name="systemMarkTable"
      />
      <ProFormText
        initialValue={props.values?.systemMarkTableColumn}
        rules={[
          {
            required: true,
            message: '双向同步标记id列名不能为空',
            max: 15,
            min: 4
          },
        ]}
        width="md"
        label="双向同步标记id列名"
        name="systemMarkTableColumn"
      />
      <ProFormText
        initialValue={props.values?.systemMarkTableInfo}
        rules={[
          {
            required: true,
            message: '双向同步标记info列名不能为空',
          },
        ]}
        width="md"
        label="双向同步标记info列名"
        name="systemMarkTableInfo"
      />
      <ProFormText
        initialValue={props.values?.systemBufferTable}
        rules={[
          {
            required: true,
            message: '同步系统buffer表不能为空',
          },
        ]}
        width="md"
        label="同步系统buffer表"
        name="systemBufferTable"
      />
      <ProFormText
        initialValue={props.values?.systemDualTable}
        rules={[
          {
            required: true,
            message: '同步系统dual表不能为空',
            max: 15,
            min: 4
          },
        ]}
        width="md"
        label="同步系统dual表"
        name="systemDualTable"
      />
      <ProFormSelect
        initialValue={props.values?.retriever || 'ARIA2C'}
        rules={[
          {
            required: true,
            message: '下载方式不能为空',
          },
        ]}
        options={[
          {
            label: 'ARIA2C',
            value: 'ARIA2C'
          }]}
        width="md"
        label="下载方式"
        name="retriever"
      />
      <ProFormText
        initialValue={(props?.values?.defaultAlarmReceiveKey || '') + (props?.values?.defaultAlarmReceiver || '')}
        rules={[
          {
            required: true,
            message: '默认报警联系人不能为空',
          },
        ]}
        transform={(value: string) => {
          let p = value?.split('=');
          return p ? {defaultAlarmReceiveKey: p[0], defaultAlarmReceiver: p[1]} : {};
        }}
        width="md"
        label="默认报警联系人"
        name="defaultAlarmReceiveKey"
      />
      <ProFormTextArea
        tooltip={<>格式:<br/> otter1=xxx@gmail.com;otter2=xxx@gmail.com，多用户请用逗号分隔，分组请用分号分隔.</>}
        width="md"
        name="desc"
        label="自定义报警联系人"
        initialValue={props?.values?.alarmReceiver? Object.entries(props.values.alarmReceiver)?.map(([k, v]) => k + '=' + v)?.join(";") : ''}
        transform={(value: string) => {
          return {
            'alarmReceiver': value?.split(';')?.reduce((prev, v) => {
              if (v.indexOf('=') > 0) {
                let [key, value] = v.split('=');
                return {...prev, [key]: value}
              }
              return prev;
            }, {})
          }
        }}
      />
    </ModalForm>
  );
};

export default UpdateForm;
